<!DOCTYPE html>

































































<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="shortcut icon" type="image/x-icon" href="../../../favicon.ico" />
<title>Testing Fundamentals | Android Developers</title>
<link href="../../../assets/android-developer-docs-devguide.css" rel="stylesheet" type="text/css" />
<script src="../../../assets/search_autocomplete.js" type="text/javascript"></script>
<script src="../../../assets/jquery-resizable.min.js" type="text/javascript"></script>
<script src="../../../assets/android-developer-docs.js" type="text/javascript"></script>
<script src="../../../assets/prettify.js" type="text/javascript"></script>
<script type="text/javascript">
  setToRoot("../../../");
</script>
<noscript>
  <style type="text/css">
    html,body{overflow:auto;}
    #body-content{position:relative; top:0;}
    #doc-content{overflow:visible;border-left:3px solid #666;}
    #side-nav{padding:0;}
    #side-nav .toggle-list ul {display:block;}
    #resize-packages-nav{border-bottom:3px solid #666;}
  </style>
</noscript>
</head>
<body class="gc-documentation">

  <div id="header">
      <div id="headerLeft">
          <a href="../../../index.html" tabindex="-1"><img
              src="../../../assets/images/bg_logo.png" alt="Android Developers" /></a>
          <ul id="header-tabs" class="guide">
    
	<li id="home-link"><a href="../../../offline.html">
	
		<span class="en">Home</span>
		<span style="display:none" class="de">Startseite</span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
		<span style="display:none" class="ja">ホーム</span>
		<span style="display:none" class="zh-CN">主页</span>
		<span style="display:none" class="zh-TW">首頁</span>
	
	</a></li>
	<li id="sdk-link"><a href="../../../sdk/index.html">
		<span class="en">SDK</span>
	</a></li>
	<li id="guide-link"><a href="../../../guide/index.html" onClick="return loadLast('guide')">
	
		<span class="en">Dev Guide</span>
		<span style="display:none" class="de">Handbuch</span>
		<span style="display:none" class="es">Guía</span>
		<span style="display:none" class="fr">Guide</span>
		<span style="display:none" class="it">Guida</span>
		<span style="display:none" class="ja">開発ガイド</span>
		<span style="display:none" class="zh-CN">开发人员指南</span>
		<span style="display:none" class="zh-TW">開發指南</span>
	
	</a></li>
	<li id="reference-link"><a href="../../../reference/packages.html" onClick="return loadLast('reference')">
	
		<span class="en">Reference</span>
		<span style="display:none" class="de">Referenz</span>
		<span style="display:none" class="es">Referencia</span>
		<span style="display:none" class="fr">Référence</span>
		<span style="display:none" class="it">Riferimento</span>
		<span style="display:none" class="ja">リファレンス</span>
		<span style="display:none" class="zh-CN">参考</span>
		<span style="display:none" class="zh-TW">參考資料</span>
	
	</a></li>
	<li id="resources-link"><a href="../../../resources/index.html" onClick="return loadLast('resources')">
	
		<span class="en">Resources</span>
		<span style="display:none" class="de"></span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
    		<span style="display:none" class="ja"></span>
		<span style="display:none" class="zh-CN"></span>
		<span style="display:none" class="zh-TW"></span>
	
	</a></li>
	<li id="videos-link"><a href="../../../videos/index.html" onClick="return loadLast('videos')">
	
		<span class="en">Videos</span>
		<span style="display:none" class="de"></span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
		<span style="display:none" class="ja">ビデオ</span>
		<span style="display:none" class="zh-CN"></span>
		<span style="display:none" class="zh-TW"></span>
	
	</a></li>
	<li><a href="http://android-developers.blogspot.com" onClick="return requestAppendHL(this.href)">
	
		<span class="en">Blog</span>
		<span style="display:none" class="de"></span>
		<span style="display:none" class="es"></span>
		<span style="display:none" class="fr"></span>
		<span style="display:none" class="it"></span>
		<span style="display:none" class="ja">ブログ</span>
		<span style="display:none" class="zh-CN">博客</span>
		<span style="display:none" class="zh-TW">網誌</span>
	
	</a></li>


     
</ul>
     
      </div>
      <div id="headerRight">
          <div id="headerLinks">
          
          <a href="http://www.android.com">Android.com</a>
          </div>
  <div id="search" >
      <div id="searchForm">
          <form accept-charset="utf-8" class="gsc-search-box" 
                onsubmit="return submit_search()">
            <table class="gsc-search-box" cellpadding="0" cellspacing="0"><tbody>
                <tr>
                  <td class="gsc-input">
                    <input id="search_autocomplete" class="gsc-input" type="text" size="33" autocomplete="off"
                      title="search developer docs" name="q"
                      value="search developer docs"
                      onFocus="search_focus_changed(this, true)"
                      onBlur="search_focus_changed(this, false)"
                      onkeydown="return search_changed(event, true, '../../../')"
                      onkeyup="return search_changed(event, false, '../../../')" />
                  <div id="search_filtered_div" class="no-display">
                      <table id="search_filtered" cellspacing=0>
                      </table>
                  </div>
                  </td>
                  <td class="gsc-search-button">
                    <input type="submit" value="Search" title="search" id="search-button" class="gsc-search-button" />
                  </td>
                  <td class="gsc-clear-button">
                    <div title="clear results" class="gsc-clear-button">&nbsp;</div>
                  </td>
                </tr></tbody>
              </table>
          </form>
      </div><!-- searchForm -->
  </div><!-- search -->
      </div><!-- headerRight -->
      <script type="text/javascript">
        <!--  
        changeTabLang(getLangPref());
        //-->
      </script>
  </div><!-- header -->

  <div class="g-section g-tpl-240" id="body-content">
    <div class="g-unit g-first" id="side-nav">
      <div id="devdoc-nav">
<ul>

  <li>
    <h2><span class="en">Android Basics</span>
        <span class="de" style="display:none">Einführung in Android</span>
        <span class="es" style="display:none">Información básica sobre Android</span>
        <span class="fr" style="display:none">Présentation d'Android</span>
        <span class="it" style="display:none">Nozioni di base su Android</span>
        <span class="ja" style="display:none">Android の基本</span>
        <span class="zh-CN" style="display:none">Android 基础知识</span>
        <span class="zh-TW" style="display:none">Android 簡介</span>
    </h2>
    <ul>
      <li><a href="../../../guide/basics/what-is-android.html">
        <span class="en">What Is Android?</span>
        <span class="de" style="display:none">Was ist Android?</span>
        <span class="es" style="display:none">¿Qué es Android?</span>
        <span class="fr" style="display:none">Qu'est-ce qu'Android&nbsp;?</span>
        <span class="it" style="display:none">Che cos'è Android?</span>
        <span class="ja" style="display:none">Android とは</span>
        <span class="zh-CN" style="display:none">Android 是什么？</span>
        <span class="zh-TW" style="display:none">什麼是 Android？</span>
          </a></li>
      <li><a href="../../../guide/topics/fundamentals.html">
        <span class="en">Application Fundamentals</span>
        <span class="de" style="display:none">Anwendungsgrundlagen</span>
        <span class="es" style="display:none">Fundamentos de las aplicaciones</span>
        <span class="fr" style="display:none">Principes de base des applications</span>
        <span class="it" style="display:none">Concetti fondamentali sulle applicazioni</span>
        <span class="ja" style="display:none">開発の基礎</span>
        <span class="zh-CN" style="display:none">应用程序基础</span>
        <span class="zh-TW" style="display:none">應用程式基本原理</span>
      </a></li>

  <!--  <li><a style="color:gray;">The Android SDK</a></li> -->
  <!--  <li><a style="color:gray;">Walkthrough for Developers</a></li> -->
      <!-- quick overview of what it's like to develop on Android -->
    </ul>
  </li>

  <li>
    <h2>
      <span class="en">Framework Topics</span>
      <span class="de" style="display:none">Framework-Themen</span>
      <span class="es" style="display:none">Temas sobre el framework</span>
      <span class="fr" style="display:none">Thèmes relatifs au framework</span>
      <span class="it" style="display:none">Argomenti relativi al framework</span>
      <span class="ja" style="display:none">フレームワーク トピック</span>
      <span class="zh-CN" style="display:none">框架主题</span>
      <span class="zh-TW" style="display:none">架構主題</span>
    </h2>
    <ul>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/fundamentals/activities.html">
          <span class="en">Activities</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/topics/fundamentals/fragments.html">
            <span class="en">Fragments</span>
          </a></li>
          <li><a href="../../../guide/topics/fundamentals/loaders.html">
            <span class="en">Loaders</span>
          </a></li>
          <li><a href="../../../guide/topics/fundamentals/tasks-and-back-stack.html">
            <span class="en">Tasks and Back Stack</span></a></li>
        </ul>
      </li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/fundamentals/services.html">
          <span class="en">Services</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/topics/fundamentals/bound-services.html">
            <span class="en">Bound Services</span>
          </a></li>
        </ul>
      </li>
      <li><a href="../../../guide/topics/providers/content-providers.html">
            <span class="en">Content Providers</span>
          </a></li>
      <li><a href="../../../guide/topics/intents/intents-filters.html">
            <span class="en">Intents and Intent Filters</span>
          </a></li>
      <li><a href="../../../guide/topics/fundamentals/processes-and-threads.html">
            <span class="en">Processes and Threads</span>
          </a></li>
    </ul>


    <ul>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/ui/index.html">
            <span class="en">User Interface</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/topics/ui/declaring-layout.html">
               <span class="en">XML Layouts</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/ui-events.html">
                <span class="en">Input Events</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/menus.html">
               <span class="en">Menus</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/actionbar.html">
               <span class="en">Action Bar</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/dialogs.html">
                <span class="en">Dialogs</span>
              </a></li>
          <li class="toggle-list">
            <div><a href="../../../guide/topics/ui/notifiers/index.html">
                <span class="en">Notifications</span>
            </a></div>
            <ul>
              <li><a href="../../../guide/topics/ui/notifiers/toasts.html">
                <span class="en">Toast Notifications</span>
              </a></li>
              <li><a href="../../../guide/topics/ui/notifiers/notifications.html">
                <span class="en">Status Bar Notifications</span>
              </a></li>
            </ul>
          </li>
          <li><a href="../../../guide/topics/ui/drag-drop.html">
                <span class="en">Drag and Drop</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/themes.html">
                <span class="en">Styles and Themes</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/custom-components.html">
                <span class="en">Custom Components</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/binding.html">
                <span class="en">Binding to Data with AdapterView</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/layout-objects.html">
                <span class="en">Common Layout Objects</span>
              </a></li>
          <li><a href="../../../guide/topics/ui/how-android-draws.html">
                <span class="en">How Android Draws Views</span>
              </a></li>
        </ul>
      </li><!-- end of User Interface -->

      <li class="toggle-list">
        <div><a href="../../../guide/topics/resources/index.html">
               <span class="en">Application Resources</span>
             </a></div>
        <ul>
          <li><a href="../../../guide/topics/resources/providing-resources.html">
                <span class="en">Providing Resources</span>
              </a></li>
          <li><a href="../../../guide/topics/resources/accessing-resources.html">
                <span class="en">Accessing Resources</span>
              </a></li>
          <li><a href="../../../guide/topics/resources/runtime-changes.html">
                <span class="en">Handling Runtime Changes</span>
              </a></li>
          <li><a href="../../../guide/topics/resources/localization.html">
                <span class="en">Localization</span>
              </a></li>
          <li class="toggle-list">
            <div><a href="../../../guide/topics/resources/available-resources.html">
              <span class="en">Resource Types</span>
            </a></div>
            <ul>
              <li><a href="../../../guide/topics/resources/animation-resource.html">Animation</a></li>
              <li><a href="../../../guide/topics/resources/color-list-resource.html">Color State List</a></li>
              <li><a href="../../../guide/topics/resources/drawable-resource.html">Drawable</a></li>
              <li><a href="../../../guide/topics/resources/layout-resource.html">Layout</a></li>
              <li><a href="../../../guide/topics/resources/menu-resource.html">Menu</a></li>
              <li><a href="../../../guide/topics/resources/string-resource.html">String</a></li>
              <li><a href="../../../guide/topics/resources/style-resource.html">Style</a></li>
              <li><a href="../../../guide/topics/resources/more-resources.html">More Types</a></li>
            </ul>
          </li><!-- end of resource types -->
        </ul>
      </li><!-- end of app resources -->
      <li class="toggle-list">
        <div><a href="../../../guide/topics/data/data-storage.html">
            <span class="en">Data Storage</span>
          </a></div>
          <ul>
            <li><a href="../../../guide/topics/data/backup.html">
                <span class="en">Data Backup</span>
              </a>
            </li>
          </ul>
      </li>
      <li><a href="../../../guide/topics/security/security.html">
            <span class="en">Security and Permissions</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/manifest/manifest-intro.html">
          <span class="en">The AndroidManifest.xml File</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/topics/manifest/action-element.html">&lt;action&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/activity-element.html">&lt;activity&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/activity-alias-element.html">&lt;activity-alias&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/application-element.html">&lt;application&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/category-element.html">&lt;category&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/compatible-screens-element.html">&lt;compatible-screens&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/data-element.html">&lt;data&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/grant-uri-permission-element.html">&lt;grant-uri-permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/instrumentation-element.html">&lt;instrumentation&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/intent-filter-element.html">&lt;intent-filter&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/manifest-element.html">&lt;manifest&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/meta-data-element.html">&lt;meta-data&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/path-permission-element.html">&lt;path-permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/permission-element.html">&lt;permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/permission-group-element.html">&lt;permission-group&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/permission-tree-element.html">&lt;permission-tree&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/provider-element.html">&lt;provider&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/receiver-element.html">&lt;receiver&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/service-element.html">&lt;service&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/supports-gl-texture-element.html">&lt;supports-gl-texture&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/supports-screens-element.html">&lt;supports-screens&gt;</a></li><!-- ##api level 4## -->
          <li><a href="../../../guide/topics/manifest/uses-configuration-element.html">&lt;uses-configuration&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/uses-feature-element.html">&lt;uses-feature&gt;</a></li> <!-- ##api level 4## -->
          <li><a href="../../../guide/topics/manifest/uses-library-element.html">&lt;uses-library&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/uses-permission-element.html">&lt;uses-permission&gt;</a></li>
          <li><a href="../../../guide/topics/manifest/uses-sdk-element.html">&lt;uses-sdk&gt;</a></li>
        </ul>
      </li><!-- end of the manifest file -->
    </ul>

    <ul>
    <li class="toggle-list">
        <div><a href="../../../guide/topics/graphics/index.html">
            <span class="en">Graphics</span>
          </a><span class="new-child">new!</span></div>
        <ul>
          <li><a href="../../../guide/topics/graphics/2d-graphics.html">
              <span class="en">Canvas and Drawables</span></a></li>
          <li><a href="../../../guide/topics/graphics/hardware-accel.html">
              <span class="en">Hardware Acceleration</span></a>
            <span class="new">new!</span></li>
          <li><a href="../../../guide/topics/graphics/opengl.html">
              <span class="en">OpenGL</span>
            </a><span class="new">updated</span></li>
        </ul>
      </li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/graphics/animation.html">
            <span class="en">Animation</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/topics/graphics/prop-animation.html">
              <span class="en">Property Animation</span></a></li>
          <li><a href="../../../guide/topics/graphics/view-animation.html">
              <span class="en">View Animation</span></a></li>
          <li><a href="../../../guide/topics/graphics/drawable-animation.html">
              <span class="en">Drawable Animation</span></a></li>
        </ul>
      </li>
      <li class="toggle-list">
	        <div><a href="../../../guide/topics/renderscript/index.html">
	            <span class="en">RenderScript</span>
	          </a></div>
	        <ul>
	          <li><a href="../../../guide/topics/renderscript/graphics.html">
	                <span class="en">Graphics</span>
	              </a>
	          </li>
	          <li><a href="../../../guide/topics/renderscript/compute.html">
	                <span class="en">Compute</span>
	              </a>
	          </li>
	        </ul>
  	  </li>

      <li class="toggle-list">
          <div><a href="../../../guide/topics/media/index.html">
            <span class="en">Multimedia and Camera</span>
          </a><span class="new">updated</span></div>
          <ul>
            <li><a href="../../../guide/topics/media/mediaplayer.html">
                  <span class="en">Media Playback</span></a>
                </li>
            <li><a href="../../../guide/topics/media/jetplayer.html">
                  <span class="en">JetPlayer</span></a>
                </li>
            <li><a href="../../../guide/topics/media/camera.html">
                  <span class="en">Camera</span></a>
                  <span class="new">new!</span>
                </li>
            <li><a href="../../../guide/topics/media/audio-capture.html">
                  <span class="en">Audio Capture</span></a>
                </li>
          </ul>
      </li>
      <li>
        <a href="../../../guide/topics/clipboard/copy-paste.html">
            <span class="en">Copy and Paste</span>
        </a></li>
  <!--<li class="toggle-list">
        <div><a style="color:gray;">Sensors</a></div>
          <ul>
            <li><a style="color:gray;">Camera</a></li>
            <li><a style="color:gray;">Compass</a></li>
            <li><a style="color:gray;">Accelerometer</a></li>
          </ul>
      </li> -->
      <li class="toggle-list">
        <div><a href="../../../guide/topics/location/index.html">
               <span class="en">Location and Maps</span>
             </a></div>
        <ul>
          <li><a href="../../../guide/topics/location/obtaining-user-location.html">
                <span class="en">Obtaining User Location</span>
              </a></li>
        </ul>
      </li>
  <!--<li class="toggle-list">
        <div><a style="color:gray;">Wireless Controls</a></div>
          <ul>
            <li><a style="color:gray;">Wi-Fi</a></li>
          </ul>
      </li> -->
  <!--<li><a style="color:gray;">Localization</a></li>  -->
      <li><a href="../../../guide/topics/appwidgets/index.html">
            <span class="en">App Widgets</span></a>
          </li>
      <li><a href="../../../guide/topics/wireless/bluetooth.html">
            <span class="en">Bluetooth</span></a>
          </li>
      <li><a href="../../../guide/topics/nfc/index.html">
            <span class="en">Near Field Communication</span>
          </a></li>
      <li class="toggle-list">
          <div><a href="../../../guide/topics/usb/index.html">
            <span class="en">USB</span></a>
          </div>
            <ul>
              <li><a href="../../../guide/topics/usb/accessory.html">Accessory</a></li>
              <li><a href="../../../guide/topics/usb/host.html">Host</a></li>
              <li><a href="../../../guide/topics/usb/adk.html">Open Accessory Dev Kit</a></li>
            </ul>
          </li>

       <li><a href="../../../guide/topics/network/sip.html">
            <span class="en">Session Initiation Protocol</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/topics/search/index.html">
            <span class="en">Search</span>
          </a></div>
          <ul>
            <li><a href="../../../guide/topics/search/search-dialog.html">Creating a Search Interface</a></li>
            <li><a href="../../../guide/topics/search/adding-recent-query-suggestions.html">Adding Recent Query Suggestions</a></li>
            <li><a href="../../../guide/topics/search/adding-custom-suggestions.html">Adding Custom Suggestions</a></li>
            <li><a href="../../../guide/topics/search/searchable-config.html">Searchable Configuration</a></li>
          </ul>
      </li>
      <li><a href="../../../guide/topics/admin/device-admin.html">
            <span class="en">Device Administration</span></a>
      </li>
      <li class="toggle-list">
           <div>
                <a href="../../../guide/topics/testing/index.html">
                   <span class="en">Testing</span>
               </a>
           </div>
           <ul>
              <li>
                <a href="../../../guide/topics/testing/testing_android.html">
                <span class="en">Testing Fundamentals</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/activity_testing.html">
                <span class="en">Activity Testing</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/contentprovider_testing.html">
                <span class="en">Content Provider Testing</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/service_testing.html">
                <span class="en">Service Testing</span></a>
              </li>
              <li>
                <a href="../../../guide/topics/testing/what_to_test.html">
                <span class="en">What To Test</span></a>
              </li>
           </ul>
      </li>
    </ul>
  </li>

  <li>
    <h2>
      <span class="en">Android Market Topics</span>
    </h2>
    <ul>
      <li><a href="../../../guide/publishing/licensing.html">
          <span class="en">Application Licensing</span></a>
      </li>
      <li class="toggle-list">
        <div><a href="../../../guide/market/billing/index.html">
            <span class="en">In-app Billing</span></a>
        </div>
        <ul>
          <li><a href="../../../guide/market/billing/billing_overview.html">
              <span class="en">In-app Billing Overview</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_integrate.html">
              <span class="en">Implementing In-app Billing</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_best_practices.html">
              <span class="en">Security and Design</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_testing.html">
              <span class="en">Testing In-app Billing</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_admin.html">
              <span class="en">Administering In-app Billing</span></a>
          </li>
          <li><a href="../../../guide/market/billing/billing_reference.html">
              <span class="en">In-app Billing Reference</span></a>
          </li>
        </ul>
      </li>
      <li><a href="../../../guide/appendix/market-filters.html">
          <span class="en">Market Filters</span></a>
      </li>
      <li><a href="../../../guide/market/publishing/multiple-apks.html">
          <span class="en">Multiple APK Support</span></a>
      </li>
    </ul>
  </li>


  <li>
    <h2><span class="en">Developing</span>
               <span class="de" style="display:none">Entwicklung</span>
               <span class="es" style="display:none">Desarrollo</span>
               <span class="fr" style="display:none">Développement</span>
               <span class="it" style="display:none">Sviluppo</span>
               <span class="ja" style="display:none">開発</span>
               <span class="zh-CN" style="display:none">开发</span>
               <span class="zh-TW" style="display:none">開發</span>
    </h2>
    <ul>
  <!--<li><a href="">Developing for Android</a></li>
      signing, upgrading, selecting a package name, select device profile, touch, trackball, dpad available, etc. -->
      <li>
        <a href="../../../guide/developing/index.html">
        <span class="en">Introduction</span></a>
      </li>

      <li class="toggle-list">
        <div>
           <a href="../../../guide/developing/devices/index.html">
                <span class="en">Managing Virtual Devices</span>
            </a>
        </div>
        <ul>
          <li>
            <a href="../../../guide/developing/devices/managing-avds.html">
              <span class="en">With AVD Manager</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/devices/managing-avds-cmdline.html">
              <span class="en">From the Command Line</span>
            </a>
          </li>
          <li>
           <a href="../../../guide/developing/devices/emulator.html">
                <span class="en">Using the Android Emulator</span>
            </a>
          </li>
        </ul>
      </li>
      <li>
        <a href="../../../guide/developing/device.html">
          <span class="en">Using Hardware Devices</span>
        </a>
      </li>

      <li class="toggle-list">
        <div>
          <a href="../../../guide/developing/projects/index.html">
            <span class="en">Managing Projects</span>
          </a>
        </div>
        <ul>
          <li>
            <a href="../../../guide/developing/projects/projects-eclipse.html">
              <span class="en">From Eclipse with ADT</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/projects/projects-cmdline.html">
                <span class="en">From the Command Line</span>
            </a>
          </li>
        </ul>
      </li>

      <li class="toggle-list">
        <div>
          <a href="../../../guide/developing/building/index.html">
            <span class="en">Building and Running</span>
          </a>
        </div>
        <ul>
          <li><a href="../../../guide/developing/building/building-eclipse.html">
            <span class="en">From Eclipse with ADT</span>
          </a></li>
          <li><a href="../../../guide/developing/building/building-cmdline.html">
            <span class="en">From the Command Line</span></a></li>
        </ul>
      </li>

      <li class="toggle-list">
        <div>
          <a href="../../../guide/developing/debugging/index.html">
            <span class="en">Debugging</span>
          </a>
        </div>
        <ul>
          <li>
            <a href="../../../guide/developing/debugging/debugging-projects.html">
                <span class="en">From Eclipse with ADT</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-projects-cmdline.html">
                <span class="en">From Other IDEs</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/ddms.html">
              <span class="en">Using DDMS</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-log.html">
                <span class="en">Reading and Writing Logs</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-ui.html">
                <span class="en">Debugging and Profiling UIs</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-tracing.html">
                <span class="en">Profiling with Traceview and dmtracedump</span>
            </a>
          </li>
          <li>
            <a href="../../../guide/developing/debugging/debugging-devtools.html">
                <span class="en">Using the Dev Tools App</span>
            </a>
          </li>
        </ul>
      </li>

      <li class="toggle-list">
           <div>
                <a href="../../../guide/developing/testing/index.html">
                   <span class="en">Testing</span>
               </a>
           </div>
           <ul>
              <li>
                <a href="../../../guide/developing/testing/testing_eclipse.html">
                  <span class="en">From Eclipse with ADT</span>
                </a>
              </li>

              <li>
                <a href="../../../guide/developing/testing/testing_otheride.html">
                  <span class="en">From Other IDEs</span>
                </a>
              </li>
           </ul>
         </li>

         <li class="toggle-list">
        <div><a href="../../../guide/developing/tools/index.html">
            <span class="en">Tools</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/developing/tools/adb.html">adb</a></li>
          <li><a href="../../../guide/developing/tools/adt.html">ADT</a></li>
          <li><a href="../../../guide/developing/tools/android.html">android</a></li>
          <li><a href="../../../guide/developing/tools/bmgr.html">bmgr</a>
          <li><a href="../../../guide/developing/tools/dmtracedump.html">dmtracedump</a></li>
          <li><a href="../../../guide/developing/tools/draw9patch.html">Draw
          9-Patch</a></li>
          <li><a href="../../../guide/developing/tools/emulator.html">Emulator</a></li>
          <li><a href="../../../guide/developing/tools/etc1tool.html">etc1tool</a></li>
          <li><a href="../../../guide/developing/tools/hierarchy-viewer.html">Hierarchy Viewer</a></li>
          <li><a href="../../../guide/developing/tools/hprof-conv.html">hprof-conv</a></li>
          <li><a href="../../../guide/developing/tools/layoutopt.html">layoutopt</a></li>
          <li><a href="../../../guide/developing/tools/logcat.html">logcat</a></li>
          <li><a href="../../../guide/developing/tools/mksdcard.html">mksdcard</a></li>
          <li><a href="../../../guide/developing/tools/monkey.html">Monkey</a></li>
          <li class="toggle-list">
            <div><a href="../../../guide/developing/tools/monkeyrunner_concepts.html">
              <span class="en">monkeyrunner</span>
            </a></div>
            <ul>
              <li><a href="../../../guide/developing/tools/MonkeyDevice.html">
                <span class="en">MonkeyDevice</span>
                </a></li>
              <li><a href="../../../guide/developing/tools/MonkeyImage.html">
                <span class="en">MonkeyImage</span>
                </a></li>
              <li><a href="../../../guide/developing/tools/MonkeyRunner.html">
                <span class="en">MonkeyRunner</span>
                </a></li>
            </ul>
          </li>
          <li><a href="../../../guide/developing/tools/proguard.html">ProGuard</a></li>
          <li><a href="../../../guide/developing/tools/adb.html#sqlite">sqlite3</a></li>
          <li><a href="../../../guide/developing/tools/traceview.html">Traceview</a></li>
          <li><a href="../../../guide/developing/tools/zipalign.html">zipalign</a></li>
        </ul>
      </li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Publishing</span>
        <span class="de" style="display:none">Veröffentlichung</span>
        <span class="es" style="display:none">Publicación</span>
        <span class="fr" style="display:none">Publication</span>
        <span class="it" style="display:none">Pubblicazione</span>
        <span class="ja" style="display:none">公開</span>
        <span class="zh-CN" style="display:none">发布</span>
        <span class="zh-TW" style="display:none">發佈</span>
    </h2>
    <ul>
      <li><a href="../../../guide/publishing/app-signing.html">
            <span class="en">Signing Your Applications</span>
            <span class="de" style="display:none">Signieren Ihrer Anwendungen</span>
            <span class="es" style="display:none">Firma de aplicaciones</span>
            <span class="fr" style="display:none">Attribution de votre signature <br />à vos applications</span>
            <span class="it" style="display:none">Firma delle applicazioni</span>
            <span class="ja" style="display:none">アプリケーションへの署名</span>
            <span class="zh-CN" style="display:none">应用程序签名</span>
            <span class="zh-TW" style="display:none">簽署應用程式</span>
          </a></li>
      <li><a href="../../../guide/publishing/versioning.html">
            <span class="en">Versioning Your Applications</span>
            <span class="de" style="display:none">Versionsverwaltung für Ihre <br />Anwendungen</span>
            <span class="es" style="display:none">Versiones de las aplicaciones</span>
            <span class="fr" style="display:none">Attribution d'une version à vos applications</span>
            <span class="it" style="display:none">Controllo versioni delle applicazioni</span>
            <span class="ja" style="display:none">アプリケーションのバージョニング</span>
            <span class="zh-CN" style="display:none">应用程序版本控制</span>
            <span class="zh-TW" style="display:none">應用程式版本設定</span>
          </a></li>
      <li><a href="../../../guide/publishing/preparing.html">
            <span class="en">Preparing to Publish</span>
            <span class="de" style="display:none">Vorbereitung auf die Veröffentlichung</span>
            <span class="es" style="display:none">Publicación de aplicaciones</span>
            <span class="fr" style="display:none">Préparation à la publication</span>
            <span class="it" style="display:none">Preparativi per la pubblicazione</span>
            <span class="ja" style="display:none">公開の準備</span>
            <span class="zh-CN" style="display:none">准备发布</span>
            <span class="zh-TW" style="display:none">準備發佈</span>
          </a></li>
      <li><a href="../../../guide/publishing/publishing.html">
            <span class="en">Publishing on Android Market</span>
          </a></li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Best Practices</span>
               <span class="de" style="display:none">Bewährte Verfahren</span>
               <span class="es" style="display:none">Prácticas recomendadas</span>
               <span class="fr" style="display:none">Meilleures pratiques</span>
               <span class="it" style="display:none">Best practice</span>
               <span class="ja" style="display:none">ベスト プラクティス</span>
               <span class="zh-CN" style="display:none">最佳实践</span>
               <span class="zh-TW" style="display:none">最佳實務</span>
    </h2>
    <ul>
      <li><a href="../../../guide/practices/compatibility.html">
            <span class="en">Compatibility</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/practices/screens_support.html">
          <span class="en">Supporting Multiple Screens</span>
        </a></div>
        <ul>
          <li><a href="../../../guide/practices/screens-distribution.html">
            <span class="en">Distributing to Specific Screens</span>
          </a></li>
          <li><a href="../../../guide/practices/screen-compat-mode.html">
            <span class="en">Screen Compatibility Mode</span>
          </a></li>
          <li><a href="../../../guide/practices/screens-support-1.5.html">
            <span class="en">Strategies for Android 1.5</span>
          </a></li>
        </ul>
      </li>
      <li><a href="../../../guide/practices/optimizing-for-3.0.html">
            <span class="en">Optimizing Apps for Android 3.0</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/practices/ui_guidelines/index.html">
               <span class="en">UI Guidelines</span>
             </a></div>
        <ul>
          <li class="toggle-list">
            <div><a href="../../../guide/practices/ui_guidelines/icon_design.html">
                   <span class="en">Icon Design <span class="new">updated</span></span>
                 </a></div>
            <ul>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_launcher.html">
                    <span class="en">Launcher Icons <span class="new">updated</span></span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_menu.html">
                    <span class="en">Menu Icons</span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_action_bar.html">
                    <span class="en">Action Bar Icons <span class="new">new!</span></span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_status_bar.html">
                    <span class="en">Status Bar Icons <span class="new">updated</span></span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_tab.html">
                    <span class="en">Tab Icons</span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_dialog.html">
                    <span class="en">Dialog Icons</span>
                  </a></li>
              <li><a href="../../../guide/practices/ui_guidelines/icon_design_list.html">
                    <span class="en">List View Icons</span>
                  </a></li>
            </ul>
          </li>
          <li><a href="../../../guide/practices/ui_guidelines/widget_design.html">
                <span class="en">App Widget Design <span class="new">updated</span></span>
              </a></li>
          <li><a href="../../../guide/practices/ui_guidelines/activity_task_design.html">
                <span class="en">Activity and Task Design</span>
              </a></li>
          <li><a href="../../../guide/practices/ui_guidelines/menu_design.html">
                <span class="en">Menu Design</span>
              </a></li>
        </ul>
      </li>
      </ul>
      <ul>
      <li><a href="../../../guide/practices/design/accessibility.html">
            <span class="en">Designing for Accessibility</span>
          </a></li>
      <li class="toggle-list">
        <div><a href="../../../guide/practices/design/performance.html">
            <span class="en">Designing for Performance</span>
          </a></div>
        <ul>
          <li><a href="../../../guide/practices/design/jni.html">
                <span class="en">JNI Tips</span>
              </a></li>
        </ul>
      </li>
      <li><a href="../../../guide/practices/design/responsiveness.html">
            <span class="en">Designing for Responsiveness</span>
          </a></li>
      <li><a href="../../../guide/practices/design/seamlessness.html">
            <span class="en">Designing for Seamlessness</span>
          </a></li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Web Applications</span>
    </h2>
    <ul>
      <li><a href="../../../guide/webapps/index.html">
            <span class="en">Web Apps Overview</span>
          </a></li>
      <li><a href="../../../guide/webapps/targeting.html">
            <span class="en">Targeting Screens from Web Apps</span>
          </a></li>
      <li><a href="../../../guide/webapps/webview.html">
            <span class="en">Building Web Apps in WebView</span>
          </a></li>
      <li><a href="../../../guide/webapps/debugging.html">
            <span class="en">Debugging Web Apps</span>
          </a></li>
      <li><a href="../../../guide/webapps/best-practices.html">
            <span class="en">Best Practices for Web Apps</span>
          </a></li>
    </ul>
  </li>

  <li>
    <h2><span class="en">Appendix</span>
               <span class="de" style="display:none">Anhang</span>
               <span class="es" style="display:none">Apéndice</span>
               <span class="fr" style="display:none">Annexes</span>
               <span class="it" style="display:none">Appendice</span>
               <span class="ja" style="display:none">付録</span>
               <span class="zh-CN" style="display:none">附录</span>
               <span class="zh-TW" style="display:none">附錄</span>
    </h2>
    <ul>
      <li><a href="../../../guide/appendix/api-levels.html">
            <span class="en">Android API Levels</span>
          </a></li>
      <li><a href="../../../guide/appendix/install-location.html">
            <span class="en">App Install Location</span>
          </a></li>
      <li><a href="../../../guide/appendix/media-formats.html">
            <span class="en">Supported Media Formats <span class="new">updated</span></span>
          </a></li>
      <li><a href="../../../guide/appendix/g-app-intents.html">
            <span class="en">Intents List: Google Apps</span>
          </a></li>
      <li><a href="../../../guide/developing/tools/aidl.html">AIDL</a></li>
      <li><a href="../../../guide/appendix/glossary.html">
            <span class="en">Glossary</span>
          </a></li>
    </ul>
  </li>

</ul>

<script type="text/javascript">
<!--
    buildToggleLists();
    changeNavLang(getLangPref());
//-->
</script>

      </div>
    </div> <!-- end side-nav -->
    <script>
      addLoadEvent(function() {
        scrollIntoView("devdoc-nav");
        });
    </script>




<div class="g-unit" id="doc-content"><a name="top"></a>

<div id="jd-header" class="guide-header">
  <span class="crumb">
    
      <a href="index.html">Testing</a> >
    
  </span>
<h1>Testing Fundamentals</h1>
</div>

  <div id="jd-content">

    <div class="jd-descr">
    <div id="qv-wrapper">
  <div id="qv">
  <h2>In this document</h2>
  <ol>
    <li>
        <a href="#TestStructure">Test Structure</a>
    </li>
    <li>
        <a href="#TestProjects">Test Projects</a>
    </li>
    <li>
      <a href="#TestAPI">The Testing API</a>
      <ol>
        <li>
          <a href="#JUnit">JUnit</a>
        </li>
        <li>
          <a href="#Instrumentation">Instrumentation</a>
        </li>
        <li>
            <a href="#TestCaseClasses">Test case classes</a>
        </li>
        <li>
          <a href="#AssertionClasses">Assertion classes</a>
        </li>
        <li>
          <a href="#MockObjectClasses">Mock object classes</a>
        </li>
      </ol>
    </li>
    <li>
        <a href="#InstrumentationTestRunner">Running Tests</a>
    </li>
    <li>
        <a href="#TestResults">Seeing Test Results</a>
    </li>
    <li>
        <a href="#Monkeys">monkey and monkeyrunner</a>
    </li>
    <li>
       <a href="#PackageNames">Working With Package Names</a>
    </li>
    <li>
        <a href="#WhatToTest">What To Test</a>
    </li>
    <li>
        <a href="#NextSteps">Next Steps</a>
    </li>
  </ol>
  <h2>Key classes</h2>
    <ol>
      <li><code><a href="../../../reference/android/test/InstrumentationTestRunner.html">InstrumentationTestRunner</a></code></li>
      <li><code><a href="../../../reference/android/test/package-summary.html">android.test</a></code></li>
      <li><code><a href="../../../reference/android/test/mock/package-summary.html">android.test.mock</a></code></li>
      <li><code><a href="../../../reference/junit/framework/package-summary.html">junit.framework</a></code></li>
    </ol>
  <h2>Related tutorials</h2>
    <ol>
        <li>
            <a href="../../../resources/tutorials/testing/helloandroid_test.html">
            Hello, Testing</a>
        </li>
        <li>
            <a href="../../../resources/tutorials/testing/activity_test.html">Activity Testing</a>
        </li>
    </ol>
  <h2>See also</h2>
      <ol>
        <li>
          <a href="../../../guide/developing/testing/testing_eclipse.html">
          Testing in Eclipse, with ADT</a>
        </li>
        <li>
          <a href="../../../guide/developing/testing/testing_otheride.html">
          Testing in Other IDEs</a>
        </li>
        <li>
          <a href="../../../guide/developing/tools/monkeyrunner_concepts.html">
          monkeyrunner</a>
        </li>
        <li>
     <a href="../../../guide/developing/tools/monkey.html">UI/Application Exerciser Monkey</a>
        </li>
      </ol>
  </div>
</div>
<p>
    The Android testing framework, an integral part of the development environment,
    provides an architecture and powerful tools that help you test every aspect of your application
    at every level from unit to framework.
</p>
<p>
    The testing framework has these key features:
</p>
<ul>
    <li>
        Android test suites are based on JUnit. You can use plain JUnit to test a class that doesn't
        call the Android API, or Android's JUnit extensions to test Android components. If you're
        new to Android testing, you can start with general-purpose test case classes such as <code><a href="../../../reference/android/test/AndroidTestCase.html">AndroidTestCase</a></code> and then go on to use more sophisticated classes.
    </li>
    <li>
        The Android JUnit extensions provide component-specific test case classes. These classes
        provide helper methods for creating mock objects and methods that help you control the
        lifecycle of a component.
    </li>
    <li>
        Test suites are contained in test packages that are similar to main application packages, so
        you don't need to learn a new set of tools or techniques for designing and building tests.
    </li>
    <li>
        The SDK tools for building and tests are available in Eclipse with ADT, and also in
        command-line form for use with other IDES. These tools get information from the project of
        the application under test and use this information to automatically create the build files,
        manifest file, and directory structure for the test package.
    </li>
    <li>
        The SDK also provides
  <a href="../../../guide/developing/tools/monkeyrunner_concepts.html">monkeyrunner</a>, an API
        testing devices with Python programs, and <a
        href="../../../guide/developing/tools/monkey.html">UI/Application Exerciser Monkey</a>,
        a command-line tool for stress-testing UIs by sending pseudo-random events to a device.
    </li>
</ul>
<p>
    This document describes the fundamentals of the Android testing framework, including the
    structure of tests, the APIs that you use to develop tests, and the tools that you use to run
    tests and view results. The document assumes you have a basic knowledge of Android application
    programming and JUnit testing methodology.
</p>
<p>
    The following diagram summarizes the testing framework:
</p>
<div style="width: 70%; margin-left:auto; margin-right:auto;">
<a href="../../../images/testing/test_framework.png">
    <img src="../../../images/testing/test_framework.png"
        alt="The Android testing framework"/>
</a>
</div>
<h2 id="TestStructure">Test Structure</h2>
<p>
    Android's build and test tools assume that test projects are organized into a standard
    structure of tests, test case classes, test packages, and test projects.
</p>
<p>
    Android testing is based on JUnit. In general, a JUnit test is a method whose
    statements test a part of the application under test. You organize test methods into classes
    called test cases (or test suites). Each test is an isolated test of an individual module in
    the application under test. Each class is a container for related test methods, although it
    often provides helper methods as well.
</p>
<p>
    In JUnit, you build one or more test source files into a class file. Similarly, in Android you
    use the SDK's build tools to build one or more test source files into class files in an
    Android test package. In JUnit, you use a test runner to execute test classes. In Android, you
    use test tools to load the test package and the application under test, and the tools then
    execute an Android-specific test runner.
</p>
<h2 id="TestProjects">Test Projects</h2>
<p>
    Tests, like Android applications, are organized into projects.
</p>
<p>
    A test project is a directory or Eclipse project in which you create the source code, manifest
    file, and other files for a test package. The Android SDK contains tools for Eclipse with ADT
    and for the command line that create and update test projects for you. The tools create the
    directories you use for source code and resources and the manifest file for the test package.
    The command-line tools also create the Ant build files you need.
</p>
<p>
    You should always use Android tools to create a test project. Among other benefits,
    the tools:
</p>
    <ul>
        <li>
            Automatically set up your test package to use
            <code><a href="../../../reference/android/test/InstrumentationTestRunner.html">InstrumentationTestRunner</a></code> as the test case runner. You must use
            <code>InstrumentationTestRunner</code> (or a subclass) to run JUnit tests.
        </li>
        <li>
            Create an appropriate name for the test package. If the application
            under test has a package name of <code>com.mydomain.myapp</code>, then the
            Android tools set the test package name to <code>com.mydomain.myapp.test</code>. This
            helps you identify their relationship, while preventing conflicts within the system.
        </li>
        <li>
            Automatically create the proper build files, manifest file, and directory
            structure for the test project. This helps you to build the test package without
            having to modify build files and sets up the linkage between your test package and
            the application under test.
            The
        </li>
    </ul>
<p>
    You can create a test project anywhere in your file system, but the best approach is to
    add the test project so that its root directory <code>tests/</code> is at the same level
    as the <code>src/</code> directory of the main application's project. This helps you find the
    tests associated with an application. For example, if your application project's root directory
    is <code>MyProject</code>, then you should use the following directory structure:
</p>
<pre class="classic no-pretty-print">
  MyProject/
      AndroidManifest.xml
      res/
          ... (resources for main application)
      src/
          ... (source code for main application) ...
      tests/
          AndroidManifest.xml
          res/
              ... (resources for tests)
          src/
              ... (source code for tests)
</pre>
<h2 id="TestAPI">The Testing API</h2>
<p>
    The Android testing API is based on the JUnit API and extended with a instrumentation
    framework and Android-specific testing classes.
</p>
<h3 id="JUnit">JUnit</h3>
<p>
    You can use the JUnit <code><a href="../../../reference/junit/framework/TestCase.html">TestCase</a></code> class to do unit testing on
    a plain Java object. <code>TestCase</code> is also the base class for
    <code><a href="../../../reference/android/test/AndroidTestCase.html">AndroidTestCase</a></code>, which you can use to test Android-dependent objects.
    Besides providing the JUnit framework, AndroidTestCase offers Android-specific setup,
    teardown, and helper methods.
</p>
<p>
    You use the JUnit <code><a href="../../../reference/junit/framework/Assert.html">Assert</a></code> class to display test results.
    The assert methods compare values you expect from a test to the actual results and
    throw an exception if the comparison fails. Android also provides a class of assertions that
    extend the possible types of comparisons, and another class of assertions for testing the UI.
    These are described in more detail in the section <a href="#AssertionClasses">
    Assertion classes</a>
</p>
<p>
    To learn more about JUnit, you can read the documentation on the
    <a href="http://www.junit.org">junit.org</a> home page.
    Note that the Android testing API supports JUnit 3 code style, but not JUnit 4. Also, you must
    use Android's instrumented test runner <code><a href="../../../reference/android/test/InstrumentationTestRunner.html">InstrumentationTestRunner</a></code> to run
    your test case classes. This test runner is described in the
    section <a href="#InstrumentationTestRunner">Running Tests</a>.
</p>
<h3 id="Instrumentation">Instrumentation</h3>
<p>
    Android instrumentation is a set of control methods or "hooks" in the Android system. These hooks
    control an Android component independently of its normal lifecycle. They also control how
    Android loads applications.
</p>
<p>
    Normally, an Android component runs in a lifecycle determined by the system. For example, an
    Activity object's lifecycle starts when the Activity is activated by an Intent. The object's
    <code>onCreate()</code> method is called, followed by <code>onResume()</code>. When the user
    starts another application, the <code>onPause()</code> method is called. If the Activity
    code calls the <code>finish()</code> method, the <code>onDestroy()</code> method is called.
    The Android framework API does not provide a way for your code to invoke these callback
    methods directly, but you can do so using instrumentation.
</p>
<p>
    Also, the system runs all the components of an application into the same
    process. You can allow some components, such as content providers, to run in a separate process,
    but you can't force an application to run in the same process as another application that is
    already running.
</p>
<p>
    With Android instrumentation, though, you can invoke callback methods in your test code.
    This allows you to run through the lifecycle of a component step by step, as if you were
    debugging the component. The following test code snippet demonstrates how to use this to
    test that an Activity saves and restores its state:
</p>
<a name="ActivitySnippet"></a>
<pre>
    // Start the main activity of the application under test
    mActivity = getActivity();

    // Get a handle to the Activity object's main UI widget, a Spinner
    mSpinner = (Spinner)mActivity.findViewById(com.android.example.spinner.R.id.Spinner01);

    // Set the Spinner to a known position
    mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION);

    // Stop the activity - The onDestroy() method should save the state of the Spinner
    mActivity.finish();

    // Re-start the Activity - the onResume() method should restore the state of the Spinner
    mActivity = getActivity();

    // Get the Spinner's current position
    int currentPosition = mActivity.getSpinnerPosition();

    // Assert that the current position is the same as the starting position
    assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition);
</pre>
<p>
    The key method used here is
    <code><a href="../../../reference/android/test/ActivityInstrumentationTestCase2.html#getActivity()">getActivity()</a></code>, which is a
    part of the instrumentation API. The Activity under test is not started until you call this
    method. You can set up the test fixture in advance, and then call this method to start the
    Activity.
</p>
<p>
    Also, instrumentation can load both a test package and the application under test into the
    same process. Since the application components and their tests are in the same process, the
    tests can invoke methods in the components, and modify and examine fields in the components.
</p>
<h3 id="TestCaseClasses">Test case classes</h3>
<p>
    Android provides several test case classes that extend <code><a href="../../../reference/junit/framework/TestCase.html">TestCase</a></code> and
    <code><a href="../../../reference/junit/framework/Assert.html">Assert</a></code> with Android-specific setup, teardown, and helper methods.
</p>
<h4 id="AndroidTestCase">AndroidTestCase</h4>
<p>
    A useful general test case class, especially if you are
    just starting out with Android testing, is <code><a href="../../../reference/android/test/AndroidTestCase.html">AndroidTestCase</a></code>. It extends
    both <code><a href="../../../reference/junit/framework/TestCase.html">TestCase</a></code> and <code><a href="../../../reference/junit/framework/Assert.html">Assert</a></code>. It provides the
    JUnit-standard <code>setUp()</code> and <code>tearDown()</code> methods, as well as well as
    all of JUnit's Assert methods. In addition, it provides methods for testing permissions, and a
    method that guards against memory leaks by clearing out certain class references.
</p>
<h4 id="ComponentTestCase">Component-specific test cases</h4>
<p>
    A key feature of the Android testing framework is its component-specific test case classes.
    These address specific component testing needs with methods for fixture setup and
    teardown and component lifecycle control. They also provide methods for setting up mock objects.
    These classes are described in the component-specific testing topics:
</p>
<ul>
    <li>
        <a href="../../../guide/topics/testing/activity_testing.html">Activity Testing</a>
    </li>
    <li>
        <a href="../../../guide/topics/testing/contentprovider_testing.html">
        Content Provider Testing</a>
    </li>
    <li>
        <a href="../../../guide/topics/testing/service_testing.html">Service Testing</a>
    </li>
</ul>
<p>
    Android does not provide a separate test case class for BroadcastReceiver. Instead, test a
    BroadcastReceiver by testing the component that sends it Intent objects, to verify that the
    BroadcastReceiver responds correctly.
</p>
<h4 id="ApplicationTestCase">ApplicationTestCase</h4>
<p>
    You use the <code><a href="../../../reference/android/test/ApplicationTestCase.html">ApplicationTestCase</a></code> test case class to test the setup and
    teardown of <code><a href="../../../reference/android/app/Application.html">Application</a></code> objects. These objects maintain the global state of
    information that applies to all the components in an application package. The test case can
    be useful in verifying that the &lt;application&gt; element in the manifest file is correctly
    set up. Note, however, that this test case does not allow you to control testing of the
    components within your application package.
</p>
<h4 id="InstrumentationTestCase">InstrumentationTestCase</h4>
<p>
    If you want to use instrumentation methods in a test case class, you must use
    <code><a href="../../../reference/android/test/InstrumentationTestCase.html">InstrumentationTestCase</a></code> or one of its subclasses. The
    <code><a href="../../../reference/android/app/Activity.html">Activity</a></code> test cases extend this base class with other functionality that
    assists in Activity testing.
</p>

<h3 id="AssertionClasses">Assertion classes</h3>
<p>
    Because Android test case classes extend JUnit, you can use assertion methods to display the
    results of tests. An assertion method compares an actual value returned by a test to an
    expected value, and throws an AssertionException if the comparison test fails. Using assertions
    is more convenient than doing logging, and provides better test performance.
</p>
<p>
    Besides the JUnit <code><a href="../../../reference/junit/framework/Assert.html">Assert</a></code> class methods, the testing API also provides
    the <code><a href="../../../reference/android/test/MoreAsserts.html">MoreAsserts</a></code> and <code><a href="../../../reference/android/test/ViewAsserts.html">ViewAsserts</a></code> classes:
</p>
<ul>
    <li>
        <code><a href="../../../reference/android/test/MoreAsserts.html">MoreAsserts</a></code> contains more powerful assertions such as
        <code><a href="../../../reference/android/test/MoreAsserts.html#assertContainsRegex(java.lang.String, java.lang.String)">assertContainsRegex(String, String)</a></code>, which does regular expression
        matching.
    </li>
    <li>
        <code><a href="../../../reference/android/test/ViewAsserts.html">ViewAsserts</a></code> contains useful assertions about Views. For example
        it contains <code><a href="../../../reference/android/test/ViewAsserts.html#assertHasScreenCoordinates(android.view.View, android.view.View, int, int)">assertHasScreenCoordinates(View, View, int, int)</a></code> that tests if a View
        has a particular X and Y position on the visible screen. These asserts simplify testing of
        geometry and alignment in the UI.
    </li>
</ul>
<h3 id="MockObjectClasses">Mock object classes</h3>
<p>
    To facilitate dependency injection in testing, Android provides classes that create mock system
    objects such as <code><a href="../../../reference/android/content/Context.html">Context</a></code> objects,
    <code><a href="../../../reference/android/content/ContentProvider.html">ContentProvider</a></code> objects, <code><a href="../../../reference/android/content/ContentResolver.html">ContentResolver</a></code>
    objects, and <code><a href="../../../reference/android/app/Service.html">Service</a></code> objects. Some test cases also provide mock
    <code><a href="../../../reference/android/content/Intent.html">Intent</a></code> objects. You use these mocks both to isolate tests
    from the rest of the system and to facilitate dependency injection for testing. These classes
    are found in the Java packages <code><a href="../../../reference/android/test/package-summary.html">android.test</a></code> and <code><a href="../../../reference/android/test/mock/package-summary.html">android.test.mock</a></code>.
</p>
<p>
    Mock objects isolate tests from a running system by stubbing out or overriding
    normal operations. For example, a <code><a href="../../../reference/android/test/mock/MockContentResolver.html">MockContentResolver</a></code>
    replaces the normal resolver framework with its own local framework, which is isolated
    from the rest of the system. MockContentResolver also also stubs out the
    <code><a href="../../../reference/android/content/ContentResolver.html#notifyChange(android.net.Uri, android.database.ContentObserver, boolean)">notifyChange(Uri, ContentObserver, boolean)</a></code> method
    so that observer objects outside the test environment are not accidentally triggered.
</p>
<p>
    Mock object classes also facilitate dependency injection by providing a subclass of the
    normal object that is non-functional except for overrides you define. For example, the
    <code><a href="../../../reference/android/test/mock/MockResources.html">MockResources</a></code> object provides a subclass of
    <code><a href="../../../reference/android/content/res/Resources.html">Resources</a></code> in which all the methods throw Exceptions when called.
    To use it, you override only those methods that must provide information.
</p>
<p>
    These are the mock object classes available in Android:
</p>
<h4 id="SimpleMocks">Simple mock object classes</h4>
<p>
    <code><a href="../../../reference/android/test/mock/MockApplication.html">MockApplication</a></code>, <code><a href="../../../reference/android/test/mock/MockContext.html">MockContext</a></code>,
    <code><a href="../../../reference/android/test/mock/MockContentProvider.html">MockContentProvider</a></code>, <code><a href="../../../reference/android/test/mock/MockCursor.html">MockCursor</a></code>,
    <code><a href="../../../reference/android/test/mock/MockDialogInterface.html">MockDialogInterface</a></code>, <code><a href="../../../reference/android/test/mock/MockPackageManager.html">MockPackageManager</a></code>, and
    <code><a href="../../../reference/android/test/mock/MockResources.html">MockResources</a></code> provide a simple and useful mock strategy. They are
    stubbed-out versions of the corresponding system object class, and all of their methods throw an
    <code><a href="../../../reference/java/lang/UnsupportedOperationException.html">UnsupportedOperationException</a></code> exception if called. To use them, you override
    the methods you need in order to provide mock dependencies.
</p>
<p class="Note"><strong>Note:</strong>
    <code><a href="../../../reference/android/test/mock/MockContentProvider.html">MockContentProvider</a></code>
    and <code><a href="../../../reference/android/test/mock/MockCursor.html">MockCursor</a></code> are new as of API level 8.
</p>
<h4 id="ResolverMocks">Resolver mock objects</h4>
<p>
    <code><a href="../../../reference/android/test/mock/MockContentResolver.html">MockContentResolver</a></code> provides isolated testing of content providers by
    masking out the normal system resolver framework. Instead of looking in the system to find a
    content provider given an authority string, MockContentResolver uses its own internal table. You
    must explicitly add providers to this table using
    <code><a href="../../../reference/android/test/mock/MockContentResolver.html#addProvider(java.lang.String, android.content.ContentProvider)">addProvider(String, ContentProvider)</a></code>.
</p>
<p>
    With this feature, you can associate a mock content provider with an authority. You can create
    an instance of a real provider but use test data in it. You can even set the provider for an
    authority to <code>null</code>. In effect, a MockContentResolver object isolates your test
    from providers that contain real data. You can control the
    function of the provider, and you can prevent your test from affecting real data.
</p>
<h3 id="ContextMocks">Contexts for testing</h3>
<p>
    Android provides two Context classes that are useful for testing:
</p>
<ul>
    <li>
        <code><a href="../../../reference/android/test/IsolatedContext.html">IsolatedContext</a></code> provides an isolated <code><a href="../../../reference/android/content/Context.html">Context</a></code>,
        File, directory, and database operations that use this Context take place in a test area.
        Though its functionality is limited, this Context has enough stub code to respond to
        system calls.
        <p>
            This class allows you to test an application's data operations without affecting real
            data that may be present on the device.
        </p>
    </li>
    <li>
        <code><a href="../../../reference/android/test/RenamingDelegatingContext.html">RenamingDelegatingContext</a></code> provides a Context in which
        most functions are handled by an existing <code><a href="../../../reference/android/content/Context.html">Context</a></code>, but
        file and database operations are handled by a <code><a href="../../../reference/android/test/IsolatedContext.html">IsolatedContext</a></code>.
        The isolated part uses a test directory and creates special file and directory names.
        You can control the naming yourself, or let the constructor determine it automatically.
        <p>
            This object provides a quick way to set up an isolated area for data operations,
            while keeping normal functionality for all other Context operations.
        </p>
    </li>
</ul>
<h2 id="InstrumentationTestRunner">Running Tests</h2>
<p>
    Test cases are run by a test runner class that loads the test case class, set ups,
    runs, and tears down each test. An Android test runner must also be instrumented, so that
    the system utility for starting applications can control how the test package
    loads test cases and the application under test. You tell the Android platform
    which instrumented test runner to use by setting a value in the test package's manifest file.
</p>
<p>
    <code><a href="../../../reference/android/test/InstrumentationTestRunner.html">InstrumentationTestRunner</a></code> is the primary Android test runner class. It
    extends the JUnit test runner framework and is also instrumented. It can run any of the test
    case classes provided by Android and supports all possible types of testing.
</p>
<p>
    You specify <code>InstrumentationTestRunner</code> or a subclass in your test package's
    manifest file, in the <a href="../../../guide/topics/manifest/instrumentation-element.html">
    instrumentation</a> element. Also, <code>InstrumentationTestRunner</code> code resides
    in the shared library <code>android.test.runner</code>,  which is not normally linked to
    Android code. To include it, you must specify it in a
    <a href="../../../guide/topics/manifest/uses-library-element.html">uses-library</a> element.
    You do not have to set up these elements yourself. Both Eclipse with ADT and the
    <code>android</code> command-line tool construct them automatically and add them to your
    test package's manifest file.
</p>
<p class="Note">
    <strong>Note:</strong> If you use a test runner other than
    <code>InstrumentationTestRunner</code>, you must change the &lt;instrumentation&gt;
    element to point to the class you want to use.
</p>
<p>
    To run <code><a href="../../../reference/android/test/InstrumentationTestRunner.html">InstrumentationTestRunner</a></code>, you use internal system classes called by
    Android tools. When you run a test in Eclipse with ADT, the classes are called automatically.
    When you run a test from the command line, you run these classes with
    <a href="../../../guide/developing/tools/adb.html">Android Debug Bridge (adb)</a>.
</p>
<p>
    The system classes load and start the test package, kill any processes that
    are running an instance of the application under test, and then load a new instance of the
    application under test. They then pass control to
    <code><a href="../../../reference/android/test/InstrumentationTestRunner.html">InstrumentationTestRunner</a></code>, which runs
    each test case class in the test package. You can also control which test cases and
    methods are run using settings in Eclipse with ADT, or using flags with the command-line tools.
</p>
<p>
    Neither the system classes nor <code><a href="../../../reference/android/test/InstrumentationTestRunner.html">InstrumentationTestRunner</a></code> run
    the application under test. Instead, the test case does this directly. It either calls methods
    in the application under test, or it calls its own methods that trigger lifecycle events in
    the application under test. The application is under the complete control of the test case,
    which allows it to set up the test environment (the test fixture) before running a test. This
    is demonstrated in the previous <a href="#ActivitySnippet">code snippet</a> that tests an
    Activity that displays a Spinner widget.
</p>
<p>
    To learn more about running tests, please read the topics
    <a href="../../../guide/developing/testing/testing_eclipse.html"">
    Testing in Eclipse, with ADT</a> or
    <a href="../../../guide/developing/testing/testing_otheride.html">
    Testing in Other IDes</a>.
</p>
<h2 id="TestResults">Seeing Test Results</h2>
<p>
    The Android testing framework returns test results back to the tool that started the test.
    If you run a test in Eclipse with ADT, the results are displayed in a new JUnit view pane. If
    you run a test from the command line, the results are displayed in <code>STDOUT</code>. In
    both cases, you see a test summary that displays the name of each test case and method that
    was run. You also see all the assertion failures that occurred. These include pointers to the
    line in the test code where the failure occurred. Assertion failures also list the expected
    value and actual value.
</p>
<p>
    The test results have a format that is specific to the IDE that you are using. The test
    results format for Eclipse with ADT is described in
    <a href="../../../guide/developing/testing/testing_eclipse.html#RunTestEclipse">
    Testing in Eclipse, with ADT</a>. The test results format for tests run from the
    command line is described in
    <a href="../../../guide/developing/testing/testing_otheride.html#RunTestsCommand">
    Testing in Other IDEs</a>.
</p>
<h2 id="Monkeys">monkey and monkeyrunner</h2>
<p>
    The SDK provides two tools for functional-level application testing:
</p>
    <ul>
        <li>
The <a href="../../../guide/developing/tools/monkey.html">UI/Application Exerciser Monkey</a>,
            usually called "monkey", is a command-line tool that sends pseudo-random streams of
            keystrokes, touches, and gestures to a device. You run it with the
            <a href="../../../guide/developing/tools/adb.html">Android Debug Bridge</a> (adb) tool.
            You use it to stress-test your application and report back errors that are encountered.
            You can repeat a stream of events by running the tool each time with the same random
            number seed.
        </li>
        <li>
    The <a href="../../../guide/developing/tools/monkeyrunner_concepts.html">monkeyrunner</a> tool
            is an API and execution environment for test programs written in Python. The API
            includes functions for connecting to a device, installing and uninstalling packages,
            taking screenshots, comparing two images, and running a test package against an
            application. Using the API, you can write a wide range of large, powerful, and complex
            tests. You run programs that use the API with the <code>monkeyrunner</code> command-line
            tool.
        </li>
    </ul>
<h2 id="PackageNames">Working With Package names</h2>
<p>
    In the test environment, you work with both Android application package names and
    Java package identifiers. Both use the same naming format, but they represent substantially
    different entities. You need to know the difference to set up your tests correctly.
</p>
<p>
    An Android package name is a unique system name for a <code>.apk</code> file, set by the
    &quot;android:package&quot; attribute of the &lt;manifest&gt; element in the package's
    manifest. The Android package name of your test package must be different from the
    Android package name of the application under test. By default, Android tools create the
    test package name by appending ".test" to the package name of the application under test.
</p>
<p>
    The test package also uses an Android package name to target the application package it
    tests. This is set in the &quot;android:targetPackage&quot; attribute of the
    &lt;instrumentation&gt; element in the test package's manifest.
</p>
<p>
    A Java package identifier applies to a source file. This package name reflects the directory
    path of the source file. It also affects the visibility of classes and members to each other.
</p>
<p>
    Android tools that create test projects set up an Android test package name for you.
    From your input, the tools set up the test package name and the target package name for the
    application under test. For these tools to work, the application project must already exist.
</p>
<p>
    By default, these tools set the Java package identifier for the test class to be the same
    as the Android package identifier. You may want to change this if you want to expose
    members in the application under test by giving them package visibility. If you do this,
    change only the Java package identifier, not the Android package names, and change only the
    test case source files. Do not change the Java package name of the generated
    <code>R.java</code> class in your test package, because it will then conflict with the
    <code>R.java</code> class in the application under test. Do not change the Android package name
    of your test package to be the same as the application it tests, because then their names
    will no longer be unique in the system.
</p>
<h2 id="WhatToTest">What to Test</h2>
<p>
    The topic <a href="../../../guide/topics/testing/what_to_test.html">What To Test</a>
    describes the key functionality you should test in an Android application, and the key
    situations that might affect that functionality.
</p>
<p>
    Most unit testing is specific to the Android component you are testing.
    The topics <a href="../../../guide/topics/testing/activity_testing.html">Activity Testing</a>,
    <a href="../../../guide/topics/testing/contentprovider_testing.html">
    Content Provider Testing</a>, and <a href="../../../guide/topics/testing/service_testing.html">
    Service Testing</a> each have a section entitled "What To Test" that lists possible testing
    areas.
</p>
<p>
    When possible, you should run these tests on an actual device. If this is not possible, you can
    use the <a href="../../../guide/developing/devices/emulator.html">Android Emulator</a> with
    Android Virtual Devices configured for the hardware, screens, and versions you want to test.
</p>
<h2 id="NextSteps">Next Steps</h2>
<p>
    To learn how to set up and run tests in Eclipse, please refer to <a
    href="../../../guide/developing/testing/testing_eclipse.html">Testing in
    Eclipse, with ADT</a>. If you're not working in Eclipse, refer to <a
    href="../../../guide/developing/testing/testing_otheride.html">Testing in Other
    IDEs</a>.
</p>
<p>
    If you want a step-by-step introduction to Android testing, try one of the
    testing tutorials or sample test packages:
</p>
<ul>
    <li>
        The <a
        href="../../../resources/tutorials/testing/helloandroid_test.html">Hello,
        Testing</a> tutorial introduces basic testing concepts and procedures in the
        context of the Hello, World application.
    </li>
    <li>
        The <a href="../../../resources/tutorials/testing/activity_test.html">Activity
        Testing</a> tutorial is an excellent follow-up to the Hello, Testing tutorial.
        It guides you through a more complex testing scenario that you develop against a
        more realistic application.
    </li>
    <!-- sample is not available
    <li>
        The sample test package <a href="../../../resources/samples/AlarmServiceTest/index.html">
        Alarm Service Test</a> is an example of testing a <code><a href="../../../reference/android/app/Service.html">Service</a></code>. It contains
        a set of unit tests for the Alarm Service sample application's <code><a href="../../../reference/android/app/Service.html">Service</a></code>.
    </li>
    -->
</ul>


    </div>

  <a href="#top" style="float:right">&uarr; Go to top</a>
  
    <p><a href="index.html">&larr; Back to Testing</a></p>
  
  </div>

<div id="footer">


  <div id="copyright">
    
  Except as noted, this content is licensed under <a
  href="http://www.apache.org/licenses/LICENSE-2.0">Apache 2.0</a>. 
  For details and restrictions, see the <a href="../../../license.html">
  Content License</a>.
  </div>
  <div id="build_info">
    
  Android 4.0&nbsp;r1 - 17 Oct 2011 18:17

  </div>

  <div id="footerlinks">
    
  <p>
    <a href="http://www.android.com/terms.html">Site Terms of Service</a> -
    <a href="http://www.android.com/privacy.html">Privacy Policy</a> -
    <a href="http://www.android.com/branding.html">Brand Guidelines</a>
  </p>
  </div>

</div> <!-- end footer -->

</div><!-- end doc-content -->

</div> <!-- end body-content --> 

<script type="text/javascript">
init(); /* initialize android-developer-docs.js */
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-5831155-1");
pageTracker._trackPageview();
</script>

</body>
</html>



